package com.addplus.townmall.server.api.mapper.exam;

import com.addplus.townmall.server.api.model.exam.Knowledge;
import com.addplus.townmall.server.api.model.exam.KnowledgeExpand;
import com.addplus.townmall.server.api.utils.BaseAddMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface KnowledgeMapper extends BaseAddMapper<Knowledge> {

    @Select("<script>\n" +
            "SELECT\n" +
            " id,\n" +
            " knowledge_name,\n" +
            " dept,\n" +
            " is_root,\n" +
            " path,\n" +
            " is_delete,\n" +
            " gmt_modified,\n" +
            " pid \n" +
            "FROM\n" +
            " knowledge \n" +
            "WHERE\n" +
            " knowledge.is_delete = 0 \n" +
            " AND path like concat('%',#{path},'%') \n"+
            " ORDER BY dept\n" +
            "</script>")
    List<KnowledgeExpand> getKnowLedgeChilderByPath(@Param("path") String path);

    @Select("SELECT id FROM knowledge where FIND_IN_SET(#{id},knowledge.path) AND is_delete = 0")
    List<Integer> getKnowledgeChildren(@Param("id") Integer id);

    @Update("UPDATE knowledge set is_delete =1 where FIND_IN_SET(#{id},knowledge.path) AND is_delete =0")
    Integer updateLogicallyDeleteChilders(@Param("id") Integer id);

    @Select("<script>\n" +
            "SELECT\n" +
            " id,\n" +
            " knowledge_name,\n" +
            " dept,\n" +
            " is_root,\n" +
            " path,\n" +
            " is_delete,\n" +
            " gmt_modified,\n" +
            " pid \n" +
            "FROM\n" +
            " knowledge \n" +
            "WHERE\n" +
            " knowledge.is_delete = 0 \n" +
            " AND path in \n" +
            " <foreach collection='knowledgeIdList' open='('  item='item'  separator=',' close=')'> #{item}</foreach> " +
            " ORDER BY dept\n" +
            "</script>")
    List<KnowledgeExpand> getKnowledgeChilderByAreaId(@Param("knowledgeIdList") List<Integer> knowledgeIdList);

    @Select("SELECT id value,knowledge_name title,dept,pid\n" +
            "FROM knowledge\n" +
            "WHERE dept = #{dept} AND is_delete = 0")
    List<KnowledgeExpand> getKnowledgeExpandByDept(Integer dept);

    @Select("SELECT id value,knowledge_name title,dept,pid\n" +
            "FROM knowledge\n" +
            "WHERE pid = #{pid} AND is_delete = 0")
    List<KnowledgeExpand> getKnowledgeExpandByPid(Integer pid);

    @Select("SELECT id,knowledge_name,dept,is_root,path,is_delete,gmt_modified,pid FROM knowledge WHERE knowledge.is_delete=0 AND pid=#{pid}")
    List<KnowledgeExpand> selectKnowledgeListByPid(@Param("pid") Integer pid);

    @Select("SELECT id,knowledge_name,dept,is_root,path,is_delete,gmt_modified,pid FROM knowledge WHERE knowledge.is_delete=0 AND pid=0")
    List<KnowledgeExpand> selectKnowledgeList();

    @Select("SELECT id,knowledge_name,dept,is_root,path,is_delete,gmt_modified,pid FROM knowledge WHERE knowledge.is_delete=0 AND knowledge.knowledge_name=#{knowledge_name}")
    List<KnowledgeExpand> getKnowledgeExpandByName(@Param("page") Page page, @Param("knowledge_name") String knowledge_name);
}
